/*
 * $Id: $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.cloth.service.programme;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.entity.AuditedObject;
import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.service.HppBaseServiceImpl;
import com.lightsaber.trade.cloth.dao.programme.ProgrammeDao;
import com.lightsaber.trade.cloth.dao.programme.StreamDao;
import com.lightsaber.trade.cloth.entity.programme.Programme;
import com.lightsaber.trade.cloth.entity.programme.Stream;

@Service
@Transactional
public class StreamManager extends HppBaseServiceImpl<Stream, String> {

    @Autowired
    private StreamDao streamDao;
    @Autowired
    private ProgrammeDao programmeDao;
    @Autowired
    private ProgrammePlanProjectionTypeManager programmePlanProjectionTypeManager;
    @Autowired
    private CurriculumHourManager curriculumHourManager;

    @Override
    protected BaseDao<Stream, String> getDao() {
        return streamDao;
    }

    public List<Stream> findByProgrammeId(final String programmeId) {
        return streamDao.findByProgrammeId(programmeId);
    }

    public void save(final Stream stream, final String programmeId) {
        if (hasSameStreamCode(programmeId, stream.getCode(), stream.getId())) {
            return;
        }

        if (StringUtils.isBlank(stream.getId())) {
            Programme programme = programmeDao.get(programmeId);
            stream.setProgramme(programme);
            super.save(stream);

            programmePlanProjectionTypeManager.onSaveNewProgramme(stream);
            curriculumHourManager.onSaveProgramme(stream);

        } else {
            Stream old = super.get(stream.getId());
            BeanUtils.copyProperties(stream, old, new String[] { BaseEntity.ID, BaseEntity.VERSION,
                    AuditedObject.CREATED_BY, AuditedObject.CREATED_DATE, "code", "duration",
                    "durationType", "programme" });

            super.save(old);
        }

    }

    public boolean hasSameStreamCode(final String programmeId, final String streamCode,
            final String streamId) {
        List<Stream> list = streamDao.countSameStreamCode(programmeId, streamCode);
        if (list.isEmpty()) {
            return false;
        } else {
            if (StringUtils.isBlank(streamId)) {
                return true;
            } else {
                Stream stream = list.get(0);
                if (stream.getId().equals(streamId))
                    return false;
                else
                    return true;
            }
        }

    }
}
